<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>BOOST_&lt;level&gt;_CLOSE</title>
<link rel="stylesheet" href="../../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Boost.Test">
<link rel="up" href="../testing_tool_ref.html" title="Reference API for writing tests">
<link rel="prev" href="assertion_boost_level_eq_collections.html" title="BOOST_&lt;level&gt;_EQUAL_COLLECTIONS">
<link rel="next" href="assertion_boost_level_close_fraction.html" title="BOOST_&lt;level&gt;_CLOSE_FRACTION">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="assertion_boost_level_eq_collections.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../testing_tool_ref.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="assertion_boost_level_close_fraction.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_test.utf_reference.testing_tool_ref.assertion_boost_level_close"></a><a class="link" href="assertion_boost_level_close.html" title="BOOST_&lt;level&gt;_CLOSE"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special">&lt;</span><span class="identifier">level</span><span class="special">&gt;</span><span class="identifier">_CLOSE</span></code></a>
</h4></div></div></div>
<pre class="programlisting"><span class="identifier">BOOST_WARN_CLOSE</span><span class="special">(</span><span class="identifier">left</span><span class="special">,</span> <span class="identifier">right</span><span class="special">,</span> <span class="identifier">tolerance</span><span class="special">);</span>
<span class="identifier">BOOST_CHECK_CLOSE</span><span class="special">(</span><span class="identifier">left</span><span class="special">,</span> <span class="identifier">right</span><span class="special">,</span> <span class="identifier">tolerance</span><span class="special">);</span>
<span class="identifier">BOOST_REQUIRE_CLOSE</span><span class="special">(</span><span class="identifier">left</span><span class="special">,</span> <span class="identifier">right</span><span class="special">,</span> <span class="identifier">tolerance</span><span class="special">);</span>
</pre>
<p>
          These tools are used to check on closeness using strong relationship defined
          by the predicate
        </p>
<pre class="programlisting"><span class="identifier">check_is_close</span><span class="special">(</span> <span class="identifier">left</span><span class="special">,</span> <span class="identifier">right</span><span class="special">,</span> <span class="identifier">tolerance</span> <span class="special">)</span></pre>
<p>
          To check for the weak relationship use <a class="link" href="assertion_boost_level_predicate.html" title="BOOST_&lt;level&gt;_PREDICATE"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special">&lt;</span><span class="identifier">level</span><span class="special">&gt;</span><span class="identifier">_PREDICATE</span></code></a> family of tools with
          explicit <code class="computeroutput"><span class="identifier">check_is_close</span></code>
          invocation.
        </p>
<p>
          The first parameter is the <span class="emphasis"><em>left</em></span> compared value. The
          second parameter is the <span class="emphasis"><em>right</em></span> compared value. Last
          third parameter defines the tolerance for the comparison in <a class="link" href="../../testing_tools/extended_comparison/floating_point.html" title="Floating point comparison"><span class="bold"><strong>percentage units</strong></span></a>.
        </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
            It is required for left and right parameters to be of the same floating
            point type. You will need to explicitly resolve any type mismatch to
            select which type to use for comparison.
          </p></td></tr>
</table></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
            The floating point comparison tools are automatically added if the <span class="emphasis"><em>Unit
            Test Framework</em></span> is included as indicated in the previous sections.
            The tools are implemented is in the header <code class="computeroutput"><a class="link" href="../../../header/boost/test/tools/floating_point_comparison_hpp.html" title="Header &lt;boost/test/tools/floating_point_comparison.hpp&gt;">boost/test/tools/floating_point_comparison.hpp</a></code>.
          </p></td></tr>
</table></div>
<h6>
<a name="boost_test.utf_reference.testing_tool_ref.assertion_boost_level_close.h0"></a>
          <span class="phrase"><a name="boost_test.utf_reference.testing_tool_ref.assertion_boost_level_close.example_descr"></a></span><a class="link" href="assertion_boost_level_close.html#boost_test.utf_reference.testing_tool_ref.assertion_boost_level_close.example_descr">Example:
          BOOST_&lt;level&gt;_CLOSE usage with small values</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">tools</span><span class="special">/</span><span class="identifier">floating_point_comparison</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">test</span> <span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">double</span> <span class="identifier">v1</span> <span class="special">=</span> <span class="number">1.23456e-10</span><span class="special">;</span>
  <span class="keyword">double</span> <span class="identifier">v2</span> <span class="special">=</span> <span class="number">1.23457e-10</span><span class="special">;</span>

  <span class="identifier">BOOST_CHECK_CLOSE</span><span class="special">(</span> <span class="identifier">v1</span><span class="special">,</span> <span class="identifier">v2</span><span class="special">,</span> <span class="number">0.0001</span> <span class="special">);</span>
  <span class="comment">// Absolute value of difference between these two values is 1e-15. They seems</span>
  <span class="comment">// to be very close. But we want to checks that these values differ no more then 0.0001%</span>
  <span class="comment">// of their value. And this test will fail at tolerance supplied.</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span>
<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">12</span><span class="special">):</span> <span class="identifier">error</span> <span class="identifier">in</span> <span class="string">"test"</span><span class="special">:</span> <span class="identifier">difference</span> <span class="identifier">between</span> <span class="identifier">v1</span><span class="special">{</span><span class="number">1.23456e-010</span><span class="special">}</span> <span class="keyword">and</span> <span class="identifier">v2</span><span class="special">{</span><span class="number">1.23457e-010</span><span class="special">}</span> <span class="identifier">exceeds</span> <span class="number">0.0001</span><span class="special">%</span>

<span class="special">***</span> <span class="number">1</span> <span class="identifier">failures</span> <span class="identifier">is</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">test</span> <span class="identifier">suite</span> <span class="string">"example"</span>
</pre>
                </td></tr></tbody>
</table></div>
<h6>
<a name="boost_test.utf_reference.testing_tool_ref.assertion_boost_level_close.h1"></a>
          <span class="phrase"><a name="boost_test.utf_reference.testing_tool_ref.assertion_boost_level_close.example_descr0"></a></span><a class="link" href="assertion_boost_level_close.html#boost_test.utf_reference.testing_tool_ref.assertion_boost_level_close.example_descr0">Example:
          BOOST_&lt;level&gt;_CLOSE usage with big values</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">tools</span><span class="special">/</span><span class="identifier">floating_point_comparison</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">test</span> <span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">double</span> <span class="identifier">v1</span> <span class="special">=</span> <span class="number">1.23456e28</span><span class="special">;</span>
  <span class="keyword">double</span> <span class="identifier">v2</span> <span class="special">=</span> <span class="number">1.23457e28</span><span class="special">;</span>

  <span class="identifier">BOOST_REQUIRE_CLOSE</span><span class="special">(</span> <span class="identifier">v1</span><span class="special">,</span> <span class="identifier">v2</span><span class="special">,</span> <span class="number">0.001</span> <span class="special">);</span>
  <span class="comment">// Absolute value of difference between these two values is 1e+23.</span>
  <span class="comment">// But we are interested only that it does not exeed 0.001% of a values compared</span>
  <span class="comment">// And this test will pass.</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span>
<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>

<span class="special">***</span> <span class="identifier">No</span> <span class="identifier">errors</span> <span class="identifier">detected</span>
</pre>
                </td></tr></tbody>
</table></div>
<p>
          See also:
        </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
              <a class="link" href="assertion_boost_level_close_fraction.html" title="BOOST_&lt;level&gt;_CLOSE_FRACTION"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special">&lt;</span><span class="identifier">level</span><span class="special">&gt;</span><span class="identifier">_CLOSE_FRACTION</span></code></a>
            </li>
<li class="listitem">
              <a class="link" href="assertion_boost_level_small.html" title="BOOST_&lt;level&gt;_SMALL"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special">&lt;</span><span class="identifier">level</span><span class="special">&gt;</span><span class="identifier">_SMALL</span></code></a>
            </li>
<li class="listitem">
              <a class="link" href="assertion_boost_level_eq.html" title="BOOST_&lt;level&gt;_EQUAL"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special">&lt;</span><span class="identifier">level</span><span class="special">&gt;</span><span class="identifier">_EQUAL</span></code></a>
            </li>
<li class="listitem">
              <a class="link" href="../../testing_tools/extended_comparison/floating_point/floating_points_comparison_theory.html" title="Theory behind floating point comparisons">Floating
              point comparison algorithms</a>
            </li>
</ul></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2022 Boost.Test contributors<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="assertion_boost_level_eq_collections.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../testing_tool_ref.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="assertion_boost_level_close_fraction.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
